let PENDING = 0
let SUCC = 1
let FAIL = 2

function WMPromise(work) {
    this.state = PENDING
    this.succCallbacks = []
    this.failCallbacks = []

    // 这里必须是异步的 ??
    // setTimeout(() => {
    work(() => {
        this.setSucc()
        this.succCallbacks.forEach(f => {
            f.call(null,)
        })
    }, () => {
        this.setFail()
        this.failCallbacks.forEach(f => {
            f.call(null,)
        })
    })
    // })
}

WMPromise.prototype.setSucc = function () {
    this.state = SUCC
}

WMPromise.prototype.setFail = function () {
    this.state = FAIL
}

WMPromise.prototype.then = function (succ, fail) {
    if (succ) {
        this.succCallbacks.push(succ)
    }
    if (fail) {
        this.failCallbacks.push(fail)
    }
    return this
};

let p = new WMPromise((resolve, rej) => {
    setTimeout(() => {
        resolve('err')
    }, 2000)
});

p.then(res => {
    console.dir('then')
}, err => {
    console.dir('err')
}).then(res => {
    console.dir('then2')
})

console.dir(1024);
console.dir(p);
